package com.elaine.service;

import com.elaine.constant.AlapiConstant;
import com.elaine.factory.alapi.AlapiFactory;
import com.elaine.itpk.ItpkReplyService;
import com.elaine.model.wx.message.BaseWechatMessage;
import com.elaine.model.wx.message.ImageMessage;
import com.elaine.utils.WechatMessageUtil;
import com.elaine.model.wx.message.TextMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author wrb
 * @date 2020/3/31 10:57
 */
@Slf4j
@Service
public class WxCallBackService {

	@Autowired
	private ItpkReplyService itpkReplyService;

	public String processRequest(HttpServletRequest request) {
		Map<String, String> map = WechatMessageUtil.xmlToMap(request);
		log.info(String.valueOf(map));
		// 发送方帐号（一个OpenID）
		String fromUserName = map.get("FromUserName");
		// 开发者微信号
		String toUserName = map.get("ToUserName");
		// 消息类型
		String msgType = map.get("MsgType");
		// 默认回复一个"success"
		String responseMessage = "success";
		// 对消息进行处理
		// 文本消息
		if (WechatMessageUtil.MESSAGE_TEXT.equals(msgType)) {
			String content = map.get("Content");
			AlapiFactory alapiFactory = AlapiFactory.factorys.get(AlapiConstant.getAlapiConstant(content));
			BaseWechatMessage message;
			// 回复图片消息
			if (WechatMessageUtil.MESSAtGE_IMAGE.equals(AlapiConstant.getAlapiConstant(content).getMessage())) {
				ImageMessage imageMessage = new ImageMessage();
				imageMessage.setMsgType(WechatMessageUtil.MESSAtGE_IMAGE);
				imageMessage.setToUserName(fromUserName);
				imageMessage.setFromUserName(toUserName);
				imageMessage.setCreateTime(System.currentTimeMillis());
				String messageResult;
				try {
					imageMessage.getImage().setMediaId(alapiFactory.getReply());
					message = imageMessage;
				} catch (Exception e) {
					log.error("Alapi返回图片失败！", e);
					if (e instanceof IllegalArgumentException) {
						messageResult = e.getMessage();
					} else {
						messageResult = "elain 心中最美的图片就是你呀~";
					}
					TextMessage textMessage = new TextMessage();
					textMessage.setMsgType(WechatMessageUtil.MESSAGE_TEXT);
					textMessage.setToUserName(fromUserName);
					textMessage.setFromUserName(toUserName);
					textMessage.setCreateTime(System.currentTimeMillis());
					textMessage.setContent(messageResult);
					message = textMessage;
				}
			}
			//回复文字消息
			else {
				TextMessage textMessage = new TextMessage();
				textMessage.setMsgType(WechatMessageUtil.MESSAGE_TEXT);
				textMessage.setToUserName(fromUserName);
				textMessage.setFromUserName(toUserName);
				textMessage.setCreateTime(System.currentTimeMillis());
				if ("菜单".equals(content)) {
					textMessage.setContent(AlapiConstant.getAlapiConstantContent());
				} else if (alapiFactory != null) {
					try {
						textMessage.setContent(alapiFactory.getReply());
					} catch (Exception e) {
						log.error("Alapi返回文本失败！", e);
						textMessage.setContent("Elain 出错了...");
					}
				} else {
					textMessage.setContent(itpkReplyService.getItpkReply(content));
//					String reply = "Elaine 看不懂QAQ，但 Elaine 猜您想要：\n\n";
//					textMessage.setContent(reply + AlapiConstant.getAlapiConstantContent().substring(AlapiConstant.getAlapiConstantContent().indexOf("\n") + 1));
				}
				message = textMessage;
			}
			responseMessage = WechatMessageUtil.messageToXml(message);
		}
		log.info(responseMessage);
		return responseMessage;

	}

}
